﻿Imports NUnit.Framework
Imports CupsApp.Service
Imports CupsApp.Data


<TestFixture()> _
Public Class CupsDomainServiceUnitTests


    <Test()> _
    Public Sub TestGetClientReturnsCorrectClients()
        Dim repo As MockClientRepository = New MockClientRepository()
        Dim srv As IClientDomainService = New CupsDomainService(repo, Nothing, Nothing, Nothing, Nothing)

        Assert.AreEqual(4, srv.GetClients().Count())
        Assert.AreEqual("FirstName1", srv.GetClients()(0).FirstName)
        Assert.AreEqual("LastName1", srv.GetClients()(0).LastName)

    End Sub

    <Test()> _
    Public Sub TestGetClientByQueryReturnsCorrectClients()
        Dim repo As MockClientRepository = New MockClientRepository()
        Dim srv As IClientDomainService = New CupsDomainService(repo, Nothing, Nothing, Nothing, Nothing)

        Dim c = From cs In srv.GetClients() _
                Where cs.ID = 1 _
                Select cs

        Assert.AreEqual("FirstName2", c.First().FirstName)

    End Sub

    <Test()> _
    Public Sub TestNewClientAddsAnotherClientToRepository()
        Dim repo As MockClientRepository = New MockClientRepository()
        Dim srv As IClientDomainService = New CupsDomainService(repo, Nothing, Nothing, Nothing, Nothing)
        Dim c As cups_Client = New cups_Client()

        c.ID = 4
        c.FirstName = "FirstName4"
        c.LastName = "LastName4"
        srv.NewClient(c)

        Assert.AreEqual(5, repo.Table().Count())

    End Sub

    <Test()> _
    Public Sub TestDeleteClientRemovesClientFromRepository()
        Dim repo As MockClientRepository = New MockClientRepository()
        Dim srv As IClientDomainService = New CupsDomainService(repo, Nothing, Nothing, Nothing, Nothing)

        Assert.IsTrue(srv.DeleteClient(2))
        Assert.AreEqual(3, repo.Table().Count())
    End Sub

    <Test()> _
    Public Sub TestGetClientReturnsClient()
        Dim repo As MockClientRepository = New MockClientRepository()
        Dim srv As IClientDomainService = New CupsDomainService(repo, Nothing, Nothing, Nothing, Nothing)

        Dim c As cups_Client = srv.GetClient(1)
        Assert.IsNotNull(c)
        Assert.AreEqual("FirstName2", c.FirstName)
    End Sub

    <Test()> _
    Public Sub TestGetClientReturnsNullWhenNoMatchingClientFound()
        Dim repo As MockClientRepository = New MockClientRepository()
        Dim srv As IClientDomainService = New CupsDomainService(repo, Nothing, Nothing, Nothing, Nothing)

        Dim c As cups_Client = srv.GetClient(666)
        Assert.IsNull(c)
    End Sub

    <Test()> _
    Public Sub TestCanAccessRelatedEntity()
        Dim repo As MockClientRepository = New MockClientRepository()
        Dim srv As IClientDomainService = New CupsDomainService(repo, Nothing, Nothing, Nothing, Nothing)

        Dim c As cups_Client = srv.GetClients()(0)
        Dim s As cups_ClientStatus = c.cups_ClientStatus()(0)

        Assert.AreEqual(0, s.ID)
    End Sub

    <Test()> _
    Public Sub TestSearchMatchesCorrectClients()
        Dim repo As MockClientRepository = New MockClientRepository()
        Dim srv As IClientDomainService = New CupsDomainService(repo, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing)

        '   Dim clients = srv.SearchClients("FirstName1", Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, True, Nothing, Nothing, Nothing)

        '    Assert.AreEqual(2, clients.Count())

        '   clients = srv.SearchClients("FirstName", Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, True, Nothing, Nothing, Nothing)

        ' Assert.AreEqual(4, clients.Count())
    End Sub

End Class
